package com.usian.service.impl;

import com.usian.district.District;
import com.usian.appointments.Appointments;
import com.usian.mapper.TravelScheduleMapper;
import com.usian.order.Order;
import com.usian.service.TravelScheduleService;
import com.usian.travelSchedule.ScenicSpotInfo;
import com.usian.travelSchedule.TravelSchedule;
import com.usian.travelSchedule.Vo.TravelScheduleVO;
import com.usian.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author 米西米西
 */
@Service
public class TravelScheduleServiceImpl implements TravelScheduleService {

    /**
     * 查询行程
     * @param id
     * @return List<TravelSchedule>
     */

    @Autowired
    TravelScheduleMapper travelScheduleMapper;

    @Override
    public TravelScheduleVO selectTravel(Integer id) {
        TravelScheduleVO travelScheduleVO = new TravelScheduleVO();

        List<TravelSchedule> travelSchedules = travelScheduleMapper.selectTravel(id);
        // 取出travelSchedules里面的景点id
        List<Integer> scenicSpotIdList = travelSchedules.stream().map(TravelSchedule::getDetailedScheduleId).collect(Collectors.toList());
        travelScheduleVO.setTravelScheduleList(travelSchedules);

        List<ScenicSpotInfo> allScenicSpotInfoList = new ArrayList<>();
        scenicSpotIdList.forEach(detailedScheduleId -> {
            List<ScenicSpotInfo> scenicSpotInfoList = travelScheduleMapper.selectScenicSpotInfo(detailedScheduleId);
            allScenicSpotInfoList.addAll(scenicSpotInfoList);
        });
        travelScheduleVO.setScenicSpotInfoList(allScenicSpotInfoList);

        return travelScheduleVO;
    }

    @Override
    public List<TravelSchedule> selectTravels(Integer id) {
        // 查找用户对应的行程信息
        List<TravelSchedule> travelSchedules = travelScheduleMapper.selectTravel(id);

        travelSchedules.forEach(travelSchedule -> {
            // 取当前行程对应的景点id
            Integer scenicSpotId = travelSchedule.getDetailedScheduleId();
            // 根据当前行程的景点id查询出对应的数据
            List<ScenicSpotInfo> scenicSpotInfoList = travelScheduleMapper.selectScenicSpotInfo(scenicSpotId);
            // 将查询到的对应景点信息设置到当前行程对象中
            travelSchedule.setScenicSpotInfoList(scenicSpotInfoList);

            // 新增代码：调用新的方法计算行程天数，并设置到实体类的对应属性中（假设实体类中有numDate属性来存储天数）
            Integer durationDays = travelScheduleMapper.calculateDurationDays(travelSchedule.getId());
            travelSchedule.setNumDate(durationDays);
        });

        return travelSchedules;
    }

    @Override
    public ScenicSpotInfo selectScenicSpotInfo(Integer id) {
        return travelScheduleMapper.selectScenicSpotInfoById(id);
    }

    @Override
    public Object save(TravelSchedule travelSchedule) {
        travelScheduleMapper.insert(travelSchedule);
        TravelSchedule  travelSchedule1 = travelScheduleMapper.selectTrav(travelSchedule.getName());
        Date startDate = travelSchedule1.getStartDate();

// 创建 SimpleDateFormat 对象，指定日期时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// 将 Date 对象格式化为字符串
        String formattedDate = sdf.format(startDate);
        Order order = new Order(null,travelSchedule1.getUserId(),null,1,1,19.9,travelSchedule1.getId(),null,null,formattedDate,null);
        return travelScheduleMapper.insertorder(order);
    }

    @Override
    public int deleteTravelSchedule(Integer id) {
        return travelScheduleMapper.deleteTravelSchedule(id);
    }

    @Override
    public int add(Appointments appointments) {
        return travelScheduleMapper.add(appointments);
    }

    @Override
    public List<Appointments> selectAppointments(Integer id) {
        return travelScheduleMapper.selectAppointments(id);
    }

    @Override
    public int deleteAppointment(Integer id) {
        return travelScheduleMapper.deleteAppointment(id);
    }


}
